using Godot;
using System;
using CurrentGame.NPC;
using CurrentGame.GUI.PlaceGrid;

namespace CurrentGame.Levels.Level2;

public partial class Level2Main : Node
{

    [Export]
    public Godot.Collections.Array<ShadeCharacter> ReadyShades { get; set; } = [];
    [Export]
    public ShadeCharacter NewShade { get; set; }
    [Export]
    public Area2D LevelEnterArea { get; set; }

    [Export]
    public DialogueConfig Dialogue1 { get; set; }
    [Export]
    public DialogueConfig Dialogue2 { get; set; }

    [Export]
    public CameraAreaConfig CameraArea1 { get; set; }


    private Player _player;
    private CinematicCamera _camera;
    private SceneSwitch _sceneSwitch;
    private PlaceModeGrid _placeGrid;
    private GamePlayGui _gamePlayGui;
    private bool _hardMode = false;

    private int _talkState = 0;

    public override void _Ready()
    {
        var map = GetNode<NodeReferenceMap>(Constants.NodeRefMapPath);
        _sceneSwitch = map.SceneSwitch;
        _sceneSwitch.SceneStart += Signal_SceneStart;
        _placeGrid = map.PlacementModeGrid;
        _player = map.MainPlayer;
        _hardMode = map.ProgressManager.HardMode;
        _gamePlayGui = map.MainGui;
        _gamePlayGui.DialogueBoxFinished += Signal_DialogueEnd;
        _gamePlayGui.SingleLineFinished += Signal_MultilineTextFinished;
        _camera = map.GameCamera;

        _player.DisablePlaceMode();
        _player.AttackEnabled = false;
        _player.PlaceModeAvailable = false;
        _player.SetHealthPoint(3);

        _placeGrid.FreePlaceCost = !_hardMode;

        if (_hardMode)
        {
            Dialogue2.Sentences = ["跳过剧情，跳过人生。"];
        }
    }

    public override void _ExitTree()
    {
        _gamePlayGui.DialogueBoxFinished -= Signal_DialogueEnd;
        _gamePlayGui.SingleLineFinished -= Signal_MultilineTextFinished;
        _sceneSwitch.SceneStart -= Signal_SceneStart;
        _placeGrid.ClearAllShade();
    }

    private void Signal_SceneStart()
    {
        GD.Print("Started Level 2");
        foreach (var shade in ReadyShades)
        {
            shade.ActivateFallDetection();
            if (_hardMode) _placeGrid.AppendNotReadyShade(shade);
            else _placeGrid.AppendReadyShade(shade);
        }
    }


    private void Signal_DialogueEnd()
    {
        if (_talkState == 21)
        {
            Dialogue2.QueueFree();
            NewShade.ActivateFallDetection();
            if (_hardMode) _placeGrid.AppendNotReadyShade(NewShade);
            else _placeGrid.AppendReadyShade(NewShade);

            _talkState = 30;
        }
    }

    private void Signal_MultilineTextFinished()
    {
        if (_talkState == 10)
        {
            if (!_hardMode)
            {
                _camera.PopCameraAreaConfig();
                _gamePlayGui.ResetFloatingAnchor();
                _gamePlayGui.CinematicMode = false;
                _player.EnableInputCheck();
            }
            _player.PlaceModeAvailable = true;
            _player.AttackEnabled = true;

            CameraArea1.QueueFree();

            _talkState = 20;
        }
    }


    private void Signal_EnterLevelAreaEnter(Area2D area)
    {
        LevelEnterArea.QueueFree();
        Dialogue1.QueueFree();

        if (!_hardMode)
        {
            _player.SkipInputCheck();
            _camera.PushCameraAreaConfig(CameraArea1);

            _gamePlayGui.CinematicMode = true;
            _gamePlayGui.SetFloatingAnchor(CameraArea1);
            _gamePlayGui.ShowMultipleText(
                [
                    " ",
                    "唉。",
                    "应该早点走的，",
                    "但好在成果转移了。",
                    "既然如此...",
                    "来尽情吞噬我吧。",
                    "我们总有一天会踩碎你这个混蛋。"
                ],
                [
                    new Vector3(3.0f, -1f, 0.3f),
                    new Vector3(2.0f, 1.0f, 0.3f),
                    new Vector3(2.0f, 20.0f, 0.3f),
                    new Vector3(2.0f, 20.0f, 0.3f),
                    new Vector3(1.0f, 5.0f, 0.1f),
                    new Vector3(3.0f, 20.0f, 0.1f),
                    new Vector3(2.0f, 20.0f, 0.3f)
                ]
            );
        }

        _talkState = 10;

        // Fake signal
        if (_hardMode) Signal_MultilineTextFinished();
    }

    private void Signal_EnsureAreaEnter(Area2D area)
    {
        if (_talkState != 30) _talkState = 21;
    }

    private void Signal_EnsureAreaLeave(Area2D area)
    {
        if (_talkState != 30) _talkState = 20;
    }
}
